package mi.payment.transaction.sale;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.sunmi.pay.hardware.aidl.AidlConstants;

import lainey.ui.utils.LogUtil;
import mi.payment.CardType;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.transaction.V2SwingCardActivity;
import mi.payment.utils.MoneyUtils;

/**
 * 消费模块 -- 刷卡页面
 */
public class ConsumeSwingCardActivity extends V2SwingCardActivity {

    private static final int mSupportAllCardType = AidlConstants.CardType.MAGNETIC.getValue()
            | AidlConstants.CardType.IC.getValue()
            | AidlConstants.CardType.NFC.getValue();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startCheckCard();
    }

    private void initView() {
        mTvTitle.setText(R.string.consume);

        String amount = getIntent().getStringExtra("extra_amount");
        long costAmount = MoneyUtils.stringMoney2LongCent(amount);
        setAmount(costAmount);

        int swingCardType = getIntent().getIntExtra("extra_check_card_type", mSupportAllCardType);
        if (CardType.NFC.getValue() == swingCardType) {
            showOnlyNfcView(true);
        } else if (CardType.IC.getValue() == swingCardType) {
            showOnlyIcView(true);
        } else {
            boolean isForceNFC = TransactionOtherOperation.getInstance().getTransactionOtherSettings().isForceNFC();
            LogUtil.e(Configs.TAG, "是否强制非接:" + isForceNFC);
            showForceNfcView(isForceNFC);
        }

        setCheckCardType(swingCardType);
    }

    @Override
    public void onCheckCardSuccess() {
        boolean isForceInputPwd = getIntent().getBooleanExtra("extra_force_input_pwd", false);
        if (CardType.NFC.getValue() != mTradeData.getPayDetail().cardType || isForceInputPwd) { // 跳转到输入密码流程
            ConsumeInputPWDActivity.startAction(this);
            finish();
        } else {
            DoubleFreeSaleController controller = new DoubleFreeSaleController(this, mTradeData);
            controller.operationProcess();
        }
    }

    public static void startAction(Context context, String amount) {
        startAction(context, amount, mSupportAllCardType, false);
    }

    public static void startAction(Context context, String amount, int swingCardType) {
        startAction(context, amount, swingCardType, false);
    }

    public static void startAction(Context context, String amount, boolean isForceInputPwd) {
        startAction(context, amount, mSupportAllCardType, isForceInputPwd);
    }

    public static void startAction(Context context, String amount, int swingCardType, boolean isForceInputPwd) {
        Intent intent = new Intent(context, ConsumeSwingCardActivity.class);
        intent.putExtra("extra_amount", amount);
        intent.putExtra("extra_check_card_type", swingCardType);
        intent.putExtra("extra_force_input_pwd", isForceInputPwd);
        context.startActivity(intent);
    }


}
